package com.greensoftcodeplus.redislog.utils;

import com.alibaba.fastjson.JSONObject;

public class ResolvingArgsArrayObject {

    public static String ResolvingArrayArgs(Object o) {
        String ret = "";
        Object object = null;
            try {

                //RPUSH
                if (o instanceof byte[][]) {
                    String jsonParam ="";
                    byte[][] a = (byte[][]) o;
                    object = ResolvingFun.UnSerializeJson((byte[]) a[0]);
                    if (object.getClass().isArray()) {
                        Object[] list= (Object[]) object;
                        int size = list.length;
                        String className = object.getClass().getName();
                        jsonParam += "[";
                        jsonParam += "\"" + className + "\"" + ",";
                        jsonParam += "[";
                        int i = 0;
                        for (Object subObject : list) {
                            jsonParam += "[";
                            String objclassName = subObject.getClass().getName();
                            jsonParam += "\"" + objclassName + "\"" + "," + JSONObject.toJSONString(subObject);
                            jsonParam += "]";
                            if (i < size - 1) jsonParam += ",";
                            i++;
                        }
                        jsonParam += "]";
                        jsonParam += "]";
                        jsonParam = jsonParam.replace("\"", "\\\"");
                        jsonParam = "\"" + jsonParam + "\"";

                        ret += jsonParam + " ";
                    }
                }


            } catch (Exception e) {
               ret="";
            }

        return ret;
    }

    public static String ResolvingOneArgs(Object o) {
        String ret = "";
        Object object = null;
        try {
//            "SREM"
            if (o instanceof byte[][]) {
                String jsonParam = "";
                byte[][] a = (byte[][]) o;
                object = ResolvingFun.UnSerializeJson((byte[]) a[0]);
                String objclassName = object.getClass().getName();
                if (!objclassName.equals("java.lang.String")) {
                    jsonParam += "[";
                    jsonParam += "\"" + objclassName + "\"" + "," + JSONObject.toJSONString(object);
                    jsonParam += "]";
                    jsonParam = jsonParam.replace("\"", "\\\"");
                    jsonParam = "\"" + jsonParam + "\"";

                }
                else
                {
                    jsonParam = ResolvingArgsArrayString.ResolvingArgs(o);
                }
                ret += jsonParam + " ";
            }
        } catch (Exception e) {
            ret = "";
        }

        return ret;
    }
}
